package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.Result;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.BrandDetailView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/7/25 0025.
 */

public class BrandDetailPersenter extends BasePresent<BrandDetailView> {

    RetrofitSerives retrofitSerives;
    Call<ResultMessage4> qrCodeCall;
    Call<Result> connectCall;

  /*  {
        "type": 38,
            "user_data": {
        "cus": 1070,
                "region_code": "110111",
                "lbs": "39.752777,116.213249",
                "b": "144",
                "native_boolean": true
    }
    }*/


    public void getJumpconnnection(int type, int userId, final String regionCode, String lbs, int brandId, boolean nativeBoolean) {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                getJumpParams(type, userId, regionCode, lbs, brandId, nativeBoolean));

        connectCall = retrofitSerives.getJumpconnnection(body);
        connectCall.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                if (response.isSuccessful()) {

                    if (response.body().getCode() == 0) {
                        view.setJumpconnnection(response.body());
                    } else {
                        view.onError("");
                        view.toast(R.string.net_request_fail);
                    }
                } else {
                    view.onError("");
                    view.toast(R.string.net_request_fail);
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                view.onError("");
                view.toast(R.string.net_request_fail);
            }
        });
    }

    public void getQrCode(String id, int factoryId, int type, final BrandDetailView view) {

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.SellerAddressTest);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), getParams(id, factoryId, type));

        qrCodeCall = retrofitSerives.getQrcode(body);

        qrCodeCall.enqueue(new Callback<ResultMessage4>() {
            @Override
            public void onResponse(Call<ResultMessage4> call, Response<ResultMessage4> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setQRCode(response.body());
                        view.hideProgress();
                    } else {
                        view.onError("");
                        view.toast(R.string.net_request_fail);
                    }
                } else {
                    view.onError("");
                    view.toast(R.string.net_request_fail);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage4> call, Throwable t) {
                view.onError("");
                view.toast(R.string.net_request_fail);
            }
        });
    }

    private String getParams(String id, int factoryId, int type) {
        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("id", id);
            paramsJson.put("user_id", factoryId);
            paramsJson.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }

    private String getJumpParams(int type, int userId, final String regionCode, String lbs, int brandId, boolean nativeBoolean) {
        JSONObject paramsJson = new JSONObject();
        try {
            paramsJson.put("type", type);

            JSONObject user_data = new JSONObject();

            user_data.put("cus", userId);
            user_data.put("region_code", regionCode);
            user_data.put("lbs", lbs);
            user_data.put("b", brandId);
            user_data.put("native_boolean", nativeBoolean);

            paramsJson.put("user_data", user_data);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return paramsJson.toString();
    }


    @Override
    public void stopRequest() {

    }
}
